package com.lzy.shops.service.impl;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.lzy.shops.form.GoodsParams;
import com.lzy.shops.mapper.PayMapper;
import com.lzy.shops.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class PayServiceImpl implements PayService {


    @Autowired
    private PayMapper mapper;

    //支付成功后要跳转的页面
    @Value("${alipay.returnUrl}")
    private String returnUrl;

    @Override
    public Object pay(String names,Double money,String orderId) {
        //从存储介质(如MySQL、Redis)查询商品信息、总金额等敏感信息
        GoodsParams params = new GoodsParams();
        //…………省略相关代码，这里直接赋值…………
        params.setGoods_name(names);
        params.setTotal(money);
        //调用sdk，发起支付
        AlipayTradePagePayResponse response = null;
        try {
            response = Factory.Payment
                    //选择网页支付平台
                    .Page()
                    //调用支付方法，设置订单名称、我们自己系统中的订单号、金额、回调页面
                    .pay(params.getGoods_name() , orderId, params.getTotal().toString() , returnUrl);
        } catch (Exception e) {
            response = null;
        }
        //这里的response.body，就是一个可以直接加载的html片段，
        // 这里为了简单我直接返回这个片段，前端直接
        return response.body;
    }

    @Override
    public Integer updateOrderState(String orderId) {
        return mapper.updateOrderState(orderId);
    }
}
